﻿Imports System.Data

Public Module DCM_HTML

    Public Function 将字符串数组转换为HTML(values() As String) As String
        Dim s_头 As String = "<html><body><p>"
        Dim s_尾 As String = "</body></p></html>"
        Dim S_中间 As String = ""
        For i As Integer = 0 To values.Length - 1
            S_中间 += values(i) & "<br />"
        Next
        Dim rsa As String = s_头 & S_中间 & s_尾
        Return rsa
    End Function

    Public Function 将对象数组转换为HTML(OBs() As Object, Optional DataTable_hrefColumns() As String = Nothing, Optional DatatableLeftColumns() As String = Nothing, Optional DatatableRightColumns() As String = Nothing, Optional FontSize As Integer = 12) As String
        Dim html_Style As String = "<style>tr,td{font-family:宋体;font-weight:100;font-size:" & FontSize & "px;line-height:14px;}.style-left-color{width:150px;text-align:left;background-color:lightgray;}.style-center-color{width:150px;text-align:center;background-color:lightgray;}.style-right-color{width:150px;text-align:right;background-color:lightgray;}.style-left-nocolor{width:150px;text-align:left;background-color:white;}.style-center-nocolor{width:150px;text-align:center;background-color:white;}.style-right-nocolor{width:150px;text-align:right;background-color:white;}</style>"
        Dim html_头 As String = "<html><body>"
        Dim html_尾 As String = "</body></html>"
        Dim HTml_中 As String = ""
        For i As Integer = 0 To OBs.Count - 1
            Select Case True
                Case OBs(i).GetType Is GetType(String)
                    HTml_中 += "<code>" & OBs(i) & "</code>"
                Case OBs(i).GetType Is GetType(String())
                    HTml_中 += 将对象数组转换为HTML_字符串数组(OBs(i))
                Case OBs(i).GetType Is GetType(DataTable)
                    HTml_中 += 将对象数组转换为HTML_DataTable(OBs(i), DataTable_hrefColumns, DatatableLeftColumns, DatatableRightColumns)
                Case OBs(i).GetType Is GetType(DataView)
                    HTml_中 += 将对象数组转换为HTML_DataView(OBs(i), DataTable_hrefColumns, DatatableLeftColumns, DatatableRightColumns)
            End Select
            HTml_中 += "<br />"
        Next
        Return html_Style & html_头 & HTml_中 & html_尾
    End Function
    Public Function 将对象数组转换为HTML_字符串数组(OBs_String() As String) As String
        Dim rs As String = ""
        For i As Integer = 0 To OBs_String.Length - 1
            rs += "<code>" & OBs_String(i) & "</code>" & "<br />"
        Next
        Return rs
    End Function
    Public Function 将对象数组转换为HTML_DataTable(DT As DataTable, DataTable_hrefColumns() As String, DatatableLeftColumns() As String, DatatableRightColumns() As String) As String
        Using DV_this = New DataView(DT, "", "", DataViewRowState.CurrentRows)
            Return 将对象数组转换为HTML_DataView(DV_this, DataTable_hrefColumns, DatatableLeftColumns, DatatableRightColumns)
        End Using
    End Function
    Public Function 将对象数组转换为HTML_DataView(DT As DataView, DataTable_hrefColumns() As String, DatatableLeftColumns() As String, DatatableRightColumns() As String) As String
        Dim rs As String = "<table border='1' cellspacing='0' cellpadding='2' bordercolor='gray' width = '100%' style='border-collapse:collapse;'>"

        For i As Integer = 0 To DT.Table.Columns.Count - 1
            rs += "<th class='auto-style4'>" & DT.Table.Columns(i).ColumnName & "</th> "
        Next
        rs += "<tr/>"
        For r As Integer = 0 To DT.Count - 1
            Dim ClassColor As String = "Color"
            If r Mod 3 = 0 Then
                ClassColor = "color"
            Else
                ClassColor = "nocolor"
            End If

            For i As Integer = 0 To DT.Table.Columns.Count - 1
                Dim ColName As String = DT.Table.Columns(i).ColumnName
                Dim IShref As Boolean = False
                If IsNothing(DataTable_hrefColumns) = False Then
                    For j As Integer = 0 To DataTable_hrefColumns.Count - 1
                        If DataTable_hrefColumns(j) = ColName Then
                            IShref = True
                            Exit For
                        End If
                    Next
                End If
                Dim Showstr As String = DCM.CaseDBNullToString(DT(r)(i))

                Dim ClassAlign As String = "center"
                If IsNothing(DatatableLeftColumns) = False Then
                    For ii As Integer = 0 To DatatableLeftColumns.Count - 1
                        If DatatableLeftColumns(ii) = ColName Then
                            ClassAlign = "left"
                            Exit For
                        End If
                    Next
                End If
                If IsNothing(DatatableRightColumns) = False Then
                    For ii As Integer = 0 To DatatableRightColumns.Count - 1
                        If DatatableRightColumns(ii) = ColName Then
                            ClassAlign = "right"
                            Exit For
                        End If
                    Next
                End If

                Dim ClsssStr = String.Format("style-{0}-{1}", ClassAlign, ClassColor)
                If IShref Then
                    rs += "<th class='" & ClsssStr & "'>" & "<a href=""" & Showstr & """>链接</a>" & "</th> "
                Else
                    rs += "<th class='" & ClsssStr & "'>" & Showstr & "</th> "
                End If

            Next
            rs += "<tr/>"
        Next
        Return rs & "</table>"
    End Function
End Module
